package d5_stream;

import org.junit.Test;

import java.nio.ByteBuffer;

public class t4NIO {
        @Test
        public void test(){
            ByteBuffer buffer = ByteBuffer.allocate(10);
            System.out.println(buffer.position());//起始位置
            System.out.println(buffer.limit());//限制
            System.out.println(buffer.capacity());//容量
            System.out.println("-------------------------------------");

            String name ="wcnm";
            buffer.put(name.getBytes());//添加字节
            System.out.println(buffer.position());//起始位置
            System.out.println(buffer.limit());//限制
            System.out.println(buffer.capacity());//容量
            System.out.println("-------------------------------------");

            buffer.flip();
            System.out.println(buffer.position());//起始位置
            System.out.println(buffer.limit());//限制
            System.out.println(buffer.capacity());//容量
            System.out.println("-------------------------------------");

            char ch =(char) buffer.get();
            System.out.println(ch);
            System.out.println(buffer.position());//起始位置
            System.out.println(buffer.limit());//限制
            System.out.println(buffer.capacity());//容量
            System.out.println("-------------------------------------");

            buffer.clear();//没清楚数据，是初始化位置
            System.out.println(buffer.position());//起始位置
            System.out.println(buffer.limit());//限制
            System.out.println(buffer.capacity());//容量
            System.out.println((char)buffer.get());
            System.out.println("-------------------------------------");

            ByteBuffer buf = ByteBuffer.allocate(10);
            String n="wcnmddd";
            buf.put(n.getBytes());

            buf.flip();

            byte[] bytes = new byte[2];
            buf.get(bytes);
            String str = new String(bytes);
            System.out.println(str);

            buf.mark();
            buf.get(bytes);
            System.out.println(new String(bytes));
            buf.reset();
            if(buf.hasRemaining()){
                System.out.println(buf.remaining());
            }
        }
        @Test
    public void test2(){
            ByteBuffer buffer = ByteBuffer.allocateDirect(1024);
            System.out.println(buffer.isDirect());
        }
}
